import { Component, OnInit, Injector, ViewChild, Output, EventEmitter } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { ModalDirective } from 'ngx-bootstrap';
import {
    DedicatedToolingServiceProxy,
    CreateOrUpdateCostOfDedicatedToolingInput,
    CostOfDedicatedToolingEditDto
} from '@shared/service-proxies/service-proxies';
import { finalize } from 'rxjs/operators';
import { CalendarModule } from 'primeng/calendar';
import { SelectItem } from 'primeng/api';

@Component({
    selector: 'createOrEditDedicatedToolingModal',
    templateUrl: './create-or-edit-dedicated-tooling-modal.component.html'
})
export class CreateOrEditDedicatedToolingModalComponent extends AppComponentBase implements OnInit {
    constructor(injector: Injector, private _dedicatedToolingServiceProxy: DedicatedToolingServiceProxy) {
        super(injector);
    }
    @ViewChild('createOrEditModal') modal: ModalDirective;

    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

    active = false;
    saving = false;
    //editDto: CostOfDeviceEditDto;
    //  extensions: Array<EditDtoExtensionsItem>;
    optionAll;
    editDto: CostOfDedicatedToolingEditDto = new CostOfDedicatedToolingEditDto();

    IdentifyType: SelectItem[];
    ngOnInit(): void {}
    show(id?: number): void {
        // this.active = true;

        // this._costZT_PowerServiceProxy.getPowerProductionForEdit(id).subscribe(result => {
        //     this.cost_PowerEditDto = result;
        //     this.modal.show();
        // });
        this.active = true;
        this._dedicatedToolingServiceProxy.getForEditAsync(id).subscribe(result => {
            this.editDto = result.costOfDedicatedTooling;

            const self = this;

            this.modal.show();
        });
    }

    save(): void {
        this.saving = true;
        let input = new CreateOrUpdateCostOfDedicatedToolingInput();
        input.costOfDedicatedTooling = this.editDto;

        this._dedicatedToolingServiceProxy
            .createOrUpdateAsync(input)
            .pipe(
                finalize(() => {
                    this.saving = false;
                })
            )
            .subscribe(() => {
                this.notify.info(this.l('SavedSuccessfully'));
                this.close();
                this.modalSave.emit(null);
            });
    }

    close(): void {
        this.active = false;
        this.modal.hide();
    }
}
